import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

import resources.Tools;
import resources.VariableAssignmentException;

/**
 * This class implements TableModelListener and sets in motion the changes to
 * valTable
 * 
 *@author Andrew Gifford 
 */
public class FormTableModelListener implements TableModelListener{

	JTable table;
	Frame frame;
	
	/**
	 * Constructor FormTableModelListener, constructs a table in the Container Pane
	 */
	public FormTableModelListener(JTable table, Frame frame){
		this.table = table;
		this.frame = frame;
	}
	
	/**
	 * Detects where the Table was changed, and tries to evaluate the entered expression
	 * 
	 * @return VOID
	 */
	public void tableChanged(TableModelEvent e){
		int firstRow = e.getFirstRow();
		int mColIndex = e.getColumn();
		
		String expression = ((FormTable)table.getModel()).dataValues[firstRow][mColIndex];
		String cellName = ((FormTable)table.getModel()).getColumnName(mColIndex) + firstRow;
		try {
			Tools.addVariable(cellName + ": " + expression);
		} catch (VariableAssignmentException e1) {
		table.setValueAt("#ERROR", firstRow, mColIndex);
		}
		frame.updateTables();
		
	}
}
